﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SimpleGUI.DetectionMethods;

namespace SimpleGUI
{
	public delegate void OutputMessageDelegate(string someMessage);
	public partial class Form1 : Form
	{
		/// <summary>
		///	The dartDetector class
		/// </summary>
		IDartDetector dartDetector;

		/// <summary>
		/// Delegate to debug some text
		/// </summary>
		OutputMessageDelegate del;

		/// <summary>
		/// Constructor
		/// </summary>
		public Form1()
		{
			InitializeComponent();
			del = new OutputMessageDelegate(OutputMessage);

			// add detection methods
			cmbDetectionMethods.Items.Add("Random");
			cmbDetectionMethods.Items.Add("Simple 2 Cameras");
			cmbDetectionMethods.SelectedIndex = 0;
		}

		/// <summary>
		/// There is a dart detected and the score was returned
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="score"></param>
		void dartDetector_DartDetected(object sender, int score)
		{
			string message = "Score: " + score.ToString();
			this.Invoke(del, new object[] { message });
		}

		/// <summary>
		/// Output message to debug textBox
		/// </summary>
		/// <param name="m"></param>
		public void OutputMessage(string m)
		{
			txtConsole.Text = m + Environment.NewLine + txtConsole.Text;
		}

		/// <summary>
		/// Start the detection of darts
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnStart_Click(object sender, EventArgs e)
		{
			if (dartDetector != null)
			{
				dartDetector.Stop();
			}
			string method = (string)cmbDetectionMethods.SelectedItem;
			if (method == "Random")
			{
				dartDetector = new SimpleGUI.DetectionMethods.RandomDart.RandomDart();
				dartDetector.DartDetected += new DartDetectionEventHandler(dartDetector_DartDetected);
				dartDetector.Start();
			}
			else if (method == "Simple 2 Cameras")
			{
				dartDetector = new SimpleGUI.DetectionMethods.Simple2Cameras.Simple2Camera();
				dartDetector.DartDetected += new DartDetectionEventHandler(dartDetector_DartDetected);
				dartDetector.Start();
			}
		}

		/// <summary>
		/// stop detecting darts
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnStop_Click(object sender, EventArgs e)
		{
			if (dartDetector != null)
				dartDetector.Stop();
		}

		/// <summary>
		/// Show / hide the interface
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSetup_Click(object sender, EventArgs e)
		{
			if (dartDetector != null)
			{
				if (dartDetector.hasInterface)
				{
					if (dartDetector.visible)
					{
						dartDetector.HideInterface();
					}
					else
					{
						dartDetector.ShowInterface();
					}
				}
				else
				{
					MessageBox.Show("Geen interface!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
			else
			{
				MessageBox.Show("Geen detectie methode gestart...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		/// <summary>
		/// When form closes, stop detecting darts
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (dartDetector != null)
				dartDetector.Stop();
		}
	}
}